package com.txw.serviceedu.controller;


import com.txw.commonutils.R;
import com.txw.serviceedu.client.VodClient;
import com.txw.serviceedu.entity.EduVideo;
import com.txw.serviceedu.service.EduVideoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author TanXiongWei
 * @since 2021-11-28
 */
@RestController
@RequestMapping("/serviceedu/edu-video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;
    @Autowired
    private VodClient vodClient;

    /**
     * 添加小节
     *
     * @param eduVideo
     * @return
     */
    @PostMapping("addVideo")
    @ApiOperation(value = "添加小节")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    /**
     * 删除小节
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "删除小节")
    public R deletVideo(@PathVariable String id) {

        //根据小节id获取视频ID，调用方法实现删除
        EduVideo byId = videoService.getById(id);
        //判断视频ID是否为空
        if (!StringUtils.isEmpty(byId.getVideoSourceId())) {
            //根据视频id，远程调用实现视频删除
            vodClient.removeAlyVideo(byId.getVideoSourceId());
        }
        videoService.removeById(id);
        return R.ok();
    }



    /**
     * 根据id查询小节
     * @param videoId
     * @return
     */
    @GetMapping("{videoId}")
    @ApiOperation(value = "根据id查询小节")
    public R seletVideo(@PathVariable String videoId) {
        EduVideo video = videoService.getById(videoId);
        return R.ok().data("video", video);
    }

    /**
     * 修改小节
     *
     * @param eduVideo
     * @return
     */
    @PostMapping("updateVideo")
    @ApiOperation(value = "修改小节")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        videoService.updateById(eduVideo);
        return R.ok();
    }


}

